// Copyright 2014 Square Inc.
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//        http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

$(document).ready(function() {
  new DynamicSearchField($('#project-filter'), processProjectSearchResults);
  new EmailAliasForm($('#email-aliases'),
                     "<%= account_emails_url format: 'json' %>",
                     "<%= account_emails_url format: 'json' %>");

  // pre-populate with most recent projects
  processProjectSearchResults('');
});

function processProjectSearchResults(query) {
  $.ajax('<%= account_memberships_url(format: 'json') %>?query=' + encodeURIComponent(query), {
    type:    'GET',
    success: function(results) {
      var resultList = $('#project-results');
      resultList.find('li').remove();
      $.each(results, function(_, membership) {
        var li = $('<li/>').appendTo(resultList);

        $('<h5/>').append($('<a/>').text(membership.project.name).attr('href', membership.project.url)).appendTo(li);
        var p = $('<p/>').text(' — ' + (membership.role == 'owner' ? 'created' : 'joined') + ' ' + membership.created_string).appendTo(li);
        if (membership.role == 'owner')
          $('<button/>').attr({href: membership.project.url + '?show_environments=true#change_owner', type: 'button'}).
                  text("Change Ownership").autoButton().prependTo(li);
        else
          $('<button/>').attr({ 'data-sqmethod': 'DELETE', href: membership.delete_url }).
                  addClass('warning').text("Leave Project").autoButton().prependTo(li);
        $('<strong/>').text(membership.human_role).prependTo(p);
      });
    },
    error:   function() { new Flash('alert').text("Error retrieving search results."); }
  });
}
